<template>
    <van-sticky>
        <van-nav-bar title="待评价" left-text="返回" left-arrow @click-left="onBack">
        </van-nav-bar>
    </van-sticky>

    <van-pull-refresh v-model="refreshing" @refresh="onRefresh" success-text="刷新成功">
        <!-- 加载列表 -->
        <van-list v-model:loading="loading" :finished="finished" :offset="10" :immediate-check="true"
            finished-text="没有更多了" @load="onLoad">
            <!-- 商品卡片 -->
            <van-card v-for="(item, key) in list" :thumb="item.products.thumb_cdn" :key="key">
                <template #title>
                    <div class="title">商品：{{ item.products.name }}</div>
                </template>

                <template #desc v-if="item.attrs">
                    <van-tag v-for="(attr, index) in JSON.parse(item.attrs)" :key="index" class="tag" type="primary">{{
                        attr.value }}</van-tag>
                </template>
                <template #desc v-else>
                    <van-tag class="tag" type="danger">无规格数据</van-tag>
                </template>

                <template #tags>
                    <div class="price">下单时间：{{ item.createtime_text }}</div>
                </template>

                <template #price>
                    <div class="price">订单总价：￥{{ item.total }}</div>
                </template>
                <template #footer>
                    <van-button type="warning" size="small" @click="onComment(item.orderid,item.proid)">评价</van-button>
                </template>
            </van-card>
        </van-list>
    </van-pull-refresh>
</template>

<script setup>
const { proxy } = getCurrentInstance();
const business = ref(proxy.$cookies.get('business') ?? {});
const list = ref([]);
const refreshing = ref(false);
const page = ref(1); //当前页面数
const listCount = ref(0); //订单个数

const onRefresh = () => {
    list.value = [];
    page.value = 1;
    finished.value = false;
    loading.value = true;

    setTimeout(onLoad, 1500);
};

const loading = ref(false);   //关闭懒加载状态
const finished = ref(false); //判断是否已下拉加载完成的状态,true完成，false没有
//懒加载
const onLoad = async () => {
    if (refreshing.value) {
        refreshing.value = false;
    }

    let params = {
        busid: business.value.id,
        page: page.value
    }

    let result = await proxy.$post({
        url: '/product/comment/clist',
        params
    })

    if (result.code === 1) {
        list.value = result.data.content;
        listCount.value = result.data.count;
        finished.value = false;
        loading.value = false;

        if (list.value.length === listCount.value) {
            finished.value = true;

            return false;
        }

        page.value = page.value + 1;

    } else {
        proxy.$showToast(result.msg);
        finished.value = true;
    }
}

//评价
const onComment = (orderid,proid) => proxy.$router.push({ path: "/product/order/proComment", query: { orderid,proid } });

const onBack = () => {
    proxy.$cookies.remove('attrs');
    proxy.$router.back();
}

</script>